<?php
namespace app\admin\controller;
use think\Controller;
use  think\Session;
use  app\admin\model\Adm as admModel;
use  app\admin\model\Order as OrderModel;
class Admin extends Controller
{

	public function index(admModel $adm,OrderModel $order)
	{

		// if(Session::get('id')){//是否有ID
		// 	$id = Session::get('id');
		// }else{
		// 	//跳转回登陆
		// }
		$id = 1;
		
		$info = $adm->get(1);

		$this->assview($info);
		$loging = $info->lden;//查询用户身份    多表查询
		 $lid   = $loging[0]['lid'];
		  //var_dump($lid);
		 $level = $adm->userpower();//查询用户权限
		 //var_dump($level);
		 $xinxi = $order->orders();//查询订单
		 $zixun = $order->zixun();//查询咨询内容
		 $xinwen =array_chunk($this->newss(),10,true);
		//$this->orderday($order);
		$this->assign('news',$xinwen[0]);//新闻API
		 $this->assign('counts',count($xinxi));//订单总数
		$this->assign('xinxi', $xinxi);//订单信息
		$this->assign('zixun', $zixun);//咨询信息
		$this->assign('zixunc', count($zixun));//咨询信息
		 $this->assign('level', $level[0]['name']);//权限
		 $this->assign('types', $loging[0]['name']);//管理员
	
		return $this->fetch();

	}
	protected function newss()//获取新闻
	{
		    $host = "http://toutiao-ali.juheapi.com";
		    $path = "/toutiao/index";
		    $method = "GET";
		    $appcode = "099e286e9e1d4474a82ecc7c76ce11de";
		    $headers = array();
		    array_push($headers, "Authorization:APPCODE " . $appcode);
		    $querys = "type=top";
		    $bodys = "";
		    $url = $host . $path . "?" . $querys;

		    $curl = curl_init();
		    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
		    curl_setopt($curl, CURLOPT_URL, $url);
		    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
		    curl_setopt($curl, CURLOPT_FAILONERROR, false);
		    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		    curl_setopt($curl, CURLOPT_HEADER, true);
		    if (1 == strpos("$".$host, "https://"))
		    {
		        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
		        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
		    }
		    $data = curl_exec($curl);
		     $re2 = substr($data,strpos($data,'{'));
 
  			$res = json_decode($re2);
  			//dump($res->result->data);
  			return $res->result->data;
		   
	}
	// protected function orderday($order)
	// {
	// 	$orders = $order->dayorder();
	// 	dump($orders);
	// }

	protected function assview($info)//渲染数据
	{
		$this->assign('icon', $info['icon']);//头像
		$this->assign('name', $info['name']);//姓名
		$this->assign('email', $info['email']);//邮箱
	}
}